package com.example.chatapp.controller;

import com.example.chatapp.common.ApiResponse;
import com.example.chatapp.model.MessageEntity;
import com.example.chatapp.service.ChatService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 历史消息控制器
 * 职责：处理历史消息查询请求
 */
@RestController
@RequestMapping("/history")
public class ChatHistoryController {
    private final ChatService chatService;

    public ChatHistoryController(ChatService chatService) {
        this.chatService = chatService;
    }

    /**
     * 获取最近消息（从Redis缓存）
     */
    @GetMapping("/recent")
    public ApiResponse<List<Object>> getRecentMessages(
            @RequestParam(defaultValue = "default") String roomId,
            @RequestParam(defaultValue = "50") int limit) {
        
        List<Object> messages = chatService.getRecentMessages(roomId, limit);
        return ApiResponse.success(messages);
    }

    /**
     * 获取增量历史消息（从MySQL）
     */
    @GetMapping("/since")
    public ApiResponse<List<MessageEntity>> getHistorySince(
            @RequestParam String roomId,
            @RequestParam Long sinceSeq) {
        
        List<MessageEntity> messages = chatService.getHistoryMessagesSince(roomId, sinceSeq);
        return ApiResponse.success(messages);
    }
}
